package com.example.mobile2021606128wje0525;

import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private EditText edtMainUserName;
    private EditText edtMainPwd;
    private CheckBox chkBoxMainPwd;
    private CheckBox chkBoxMainLogin;
    private Button btnMainLogin;
    private Button btnMainRegedit;
    private String url = "http://121.9.253.237:8888/traffic/api/v2/user_login";
OkHttpClient okHttpClient=new OkHttpClient();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        initView();
        btnMainLogin.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName=edtMainUserName.getText().toString().trim();
                String password =edtMainPwd.getText().toString().trim();

                new Thread( new Runnable() {
                    @Override
                    public void run() {
                        //参数内容
                        JsonObject jsonObject=new JsonObject();
                        jsonObject.addProperty("UserName",password);
                        jsonObject.addProperty("UserPwd",password);
                        String json= jsonObject.toString();
                        //参数格式
                        MediaType mediaType=MediaType.Companion.parse( "application/json;charset=utf-8" );
                        RequestBody requestBody=RequestBody.create( json,mediaType );

                        //创建request对象
                        Request request=new Request.Builder(  )
                                .url( url )
                                .build();

                        //将请求request设置给Okhttp客户端访问对象。创建call
                        Call call=okHttpClient.newCall( request );

                        //异步访问,将call加到执行队列
                        call.enqueue( new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result=response.body().string();
                                Log.i( "TAG", "onResponse: "+result );
                                Gson gson=new Gson();
                                LoginBean loginBean=gson.fromJson( result,LoginBean.class );
                                Log.i( "TAG", "onResponse: "+loginBean.getRESULT() );

                                runOnUiThread( new Runnable() {
                                    @Override
                                    public void run() {
                                        if (loginBean.getRESULT().equals( "S" )){
                                            new AlertDialog.Builder( MainActivity.this )
                                                    .setMessage( "登录成功！" )
                                                    .create()
                                                    .show();
                                        }else   if (loginBean.getRESULT().equals( "F" )){
                                            new AlertDialog.Builder( MainActivity.this )
                                                    .setMessage( "登录失败" )
                                                    .create()
                                                    .show();
                                        }


                                    }
                                } );

                            }
                        } );




                    }
                } ).start();
            }
        } );
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById( R.id.tvMainTitle );
        edtMainUserName = (EditText) findViewById( R.id.edtMainUserName );
        edtMainPwd = (EditText) findViewById( R.id.edtMainPwd );
        chkBoxMainPwd = (CheckBox) findViewById( R.id.chkBoxMainPwd );
        chkBoxMainLogin = (CheckBox) findViewById( R.id.chkBoxMainLogin );
        btnMainLogin = (Button) findViewById( R.id.btnMainLogin );
        btnMainRegedit = (Button) findViewById( R.id.btnMainRegedit );
    }
}